package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.util.locking;

import br.edu.ufcg.lsd.wbis.ui.client.services.GeneralService;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.PanelManager;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Renato Miceli
 */
public class UnlockingButtonListener extends ButtonListenerAdapter {

	private final UnlockingWindow unlockingWindow;
	private final UnlockingFormPanel unlockingPanel;
	private final PanelManager panelManager;

	/**
	 * @param unlockingWindow
	 * @param unlockingPanel
	 * @param panelManager
	 */
	public UnlockingButtonListener(UnlockingWindow unlockingWindow, UnlockingFormPanel unlockingPanel, PanelManager panelManager) {
		this.unlockingWindow = unlockingWindow;
		this.unlockingPanel = unlockingPanel;
		this.panelManager = panelManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(Button button, EventObject e) {
		final String password = this.unlockingPanel.parseForm().getPassword();
		if (password == null || password.isEmpty()) {
			MessageBox.alert("Password error", "The typed password must not be empty!");
		} else {
			GeneralService.Util.getInstance().login(this.unlockingPanel.parseForm(),
					new UnlockingLoginCallback(this.unlockingWindow, this.panelManager));
		}
	}

}
